
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.lang.Graphics;

public class GraphPanel extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JTextField startAngle, endAngle, thetaCoeff; 
	private JLabel message;
	private GraphDisplay graphArea;
	
	public GraphPanel() {
		setBackground(Color.WHITE);
		setBorder(BorderFactory.createLineBorder(new Color(69,125,151)));
		
		//Graph display area
		graphArea = new GraphDisplay();
		graphArea.setPreferredSize(new Dimension(450, 310));
		
		//Text fields 
		startAngle = new JTextField(null, 10);
		startAngle.setBackground(Color.WHITE);
		endAngle = new JTextField(null, 10);
		endAngle.setBackground(Color.WHITE);
		thetaCoeff = new JTextField(null, 10);
		thetaCoeff.setBackground(Color.WHITE);
		
		//Labels for angles text fields
		JPanel anglesPanel = new JPanel();

		JLabel angleLabel = new JLabel("Angles: ", JLabel.LEFT); 
		anglesPanel.add(angleLabel);
		
		JPanel startAnglePanel = new JPanel();
		startAnglePanel.add(new JLabel("from "));
		startAnglePanel.add(startAngle);
		anglesPanel.add(startAnglePanel);
		
		JPanel endAnglePanel = new JPanel();
		endAnglePanel.add(new JLabel("to "));
		endAnglePanel.add(endAngle);
		anglesPanel.add(endAnglePanel);
		
		//Coefficient of theta text field
		JPanel thetaPanel = new JPanel();
		thetaPanel.add(new JLabel("Coefficient of the angle ", JLabel.LEFT));
		thetaPanel.add(thetaCoeff);
		
		//Graph button
		JButton graphButton = new JButton("Graph!");
		graphButton.addActionListener(this);
		thetaPanel.add(graphButton);
		
		//Panel with inputs.
		JPanel inputs = new JPanel();

		//Set up label for equation and error message.
		message = new JLabel ("", JLabel.CENTER);
		message.setFont(new Font("Serif", Font.BOLD, 14));
		inputs.add(message);
		
		inputs.add(anglesPanel);
		inputs.add(thetaPanel);
		inputs.setPreferredSize(new Dimension(450, 150));
		inputs.setBorder(BorderFactory.createLineBorder(new Color(69,125,151)));
		
		//Layout the graph, text fields and button
		add(graphArea);
		add(inputs);
	}
	
	public void actionPerformed(ActionEvent e) {
		double start, end, coeff;
		String warning = "Illegal data entry, please enter a number.";
		
		try {
			String startStr = startAngle.getText();
			start = Double.parseDouble(startStr);
		}
		catch (NumberFormatException s) {
			message.setText(warning);
			graphArea.validate();
			startAngle.requestFocus();
			return;
		}
		
		try {
			String endStr = endAngle.getText();
			end = Double.parseDouble(endStr);
		}
		catch (NumberFormatException s) {
			message.setText(warning);
			graphArea.validate();
			endAngle.requestFocus();
			return;
		}
		
		try {
			String coeffStr = thetaCoeff.getText();
			coeff = Double.parseDouble(coeffStr);
		}
		catch (NumberFormatException s) {
			message.setText(warning);
			graphArea.validate();
			thetaCoeff.requestFocus();
			return;
		}
		//Display the equation
		message.setText("r = cos(" + coeff + "\u03B8" + ")");
		graphArea.plot(start, end, coeff);
	}
}
